package com.oa.modules.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.oa.common.utils.AppUtils;
import com.oa.common.utils.PageUtils;
import com.oa.common.utils.Query;
import com.oa.modules.sys.dao.SysDeptDao;
import com.oa.modules.sys.entity.SysDeptEntity;
import com.oa.modules.sys.service.SysDeptService;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("sysDeptService")
public class SysDeptServiceImpl extends ServiceImpl<SysDeptDao, SysDeptEntity> implements SysDeptService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        //IPage<SysDeptEntity> page = this.page(
        //        new Query<SysDeptEntity>().getPage(params),
        //        new QueryWrapper<SysDeptEntity>().eq("tenant_id", params.get("tenantId"))
        //);
        return new PageUtils(this.page(new Query<SysDeptEntity>().getPage(params), AppUtils.setQueryWrapper(new QueryWrapper<SysDeptEntity>(), AppUtils.checkMap(params), "create_time", false)));

    }

    /**
     * 新增部门方法
     *
     * @param sysDept
     */
    @Override
    public void insertDept(SysDeptEntity sysDept) {
        SysDeptEntity info = baseMapper.selectById(sysDept.getParentId());
        //if (info.getAncestors() != null && sysDept.getParentId() != null) {
        //    sysDept.setAncestors(info.getAncestors() + "," + sysDept.getParentId());
        //}
        baseMapper.insert(sysDept);
    }

    /**
     * 修改保存部门信息
     *
     * @param sysDept
     */
    @Override
    public void updateDept(SysDeptEntity sysDept) {

        SysDeptEntity info = baseMapper.selectById(sysDept.getParentId());
        if (info != null) {
            String ancestors = info.getAncestors() + "," + sysDept.getParentId();
            sysDept.setAncestors(ancestors);
            updateDeptChildren(sysDept.getDeptId(), ancestors);
        }
        baseMapper.updateById(sysDept);
    }

    /**
     * 修改子元素关系
     *
     * @param deptId
     * @param ancestors
     */
    public void updateDeptChildren(String deptId, String ancestors) {
        SysDeptEntity sysDept = new SysDeptEntity();
        sysDept.setParentId(deptId);

        // 查询子级部门,然后修改子级部门的父级部门数据
        List<SysDeptEntity> childrens = baseMapper.selectDeptList(sysDept);
        for (SysDeptEntity children : childrens) {
            children.setAncestors(ancestors + "," + sysDept.getParentId());
        }
        if (childrens.size() > 0) {
            baseMapper.updateDeptChildren(childrens);
        }
    }

    /**
     * 查询公司介绍
     * @param deptId
     */
    public SysDeptEntity qryDeptIntroduce(String deptId){
        SysDeptEntity sysDept = baseMapper.qryDeptIntroduce(deptId);
        return sysDept;
    }
    /**
     * 查询商铺下的订单数量
     */
    public List<Map<String,Object>> qryShopView(Map<String,Object> map){
        return baseMapper.qryShopView(map);
    }

}